using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Preferences;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using NorthStar.Passenger.Application;
using TheNorthStar.Api.Requests;
using TheNorthStar.Api.Results;

namespace NorthStar.Passenger
{
    [Activity(Label = "My Booking")]
    public class MyBookingActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
           
            Booking booking = null;
            ISharedPreferences _preferences = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
            string bookingJson = _preferences.GetString("lastBookingRef", string.Empty);
            if (bookingJson != string.Empty)
            {
                booking = Newtonsoft.Json.JsonConvert.DeserializeObject<Booking>(bookingJson);

                if (booking.Status == "CONFIRMED")
                {
                    SetContentView(Resource.Layout.MyBooking);
                    TextView refNumberLabel = FindViewById<TextView>(Resource.Id.NewBookingProcessingRefNumberLabel);
                    refNumberLabel.Text = booking.BookingId.ToString();

                    TextView driverNameLabel = FindViewById<TextView>(Resource.Id.DriverNameLabel);
                    driverNameLabel.Text = string.Format("Your driver is: {0}",booking.DriverName);

                    Button callDriverBtn = FindViewById<Button>(Resource.Id.CallMyDriverBtn);
                    callDriverBtn.Click += (sender, e) =>
                                               {
                                                   Intent callIntent = new Intent(Intent.ActionCall);
                                                   callIntent.SetData(Android.Net.Uri.Parse(string.Format("tel:{0}",booking.DriverPhoneNumber)));
                                                   StartActivity(callIntent);
                                               };

                    Button rateDriverBtn = FindViewById<Button>(Resource.Id.RateMyDriverBtn);
                    rateDriverBtn.Click += (sender, e) =>
                                               {
                                                   Intent a = new Intent(this, typeof(RateDriverActivity));
                                                   a.PutExtra("refId", booking.BookingId);
                                                   StartActivity(a);
                                               };

                    Button cancelBookingBtn = FindViewById<Button>(Resource.Id.CancelBookingBtn);
                    cancelBookingBtn.Click += (sender, e) =>
                                                  {

                                                      CancelBookingRequest cancelBooking = new CancelBookingRequest();
                                                      cancelBooking.IMEI = Helper.GetDeviceId(ApplicationContext);
                                                      cancelBooking.PhoneNumber = Helper.GetPhoneNumber(ApplicationContext);
                                                      cancelBooking.isCancelled = true;
                                                      cancelBooking.BookingId = booking.BookingId;


                                                      CancelBookingAsync asyncTask = new CancelBookingAsync(this, cancelBooking);

                                                      asyncTask.Execute();



                                                  };
                }
                else if(booking.Status == "NEW")
                {
                    SetContentView(Resource.Layout.NewBookingProcessingLayout);
                    TextView refNumberLabel = FindViewById<TextView>(Resource.Id.NewBookingProcessingRefNumberLabel);
                    refNumberLabel.Text = booking.BookingId.ToString();

                    Button continueBtn = FindViewById<Button>(Resource.Id.NewBookingProcessingContinueBtn);
                    continueBtn.Click += (sender, e) => { this.Finish(); };
                    
                }
                else
                {
                    CreateAlert();
                }
            }
            else
            {
              CreateAlert();




            }
            //ApiService api = new ApiService();
            
           
        }

        private void CreateAlert()
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.SetTitle("No Booking")
                .SetMessage("You must have a active booking. Would you like to create one now?")
                .SetPositiveButton("Yes",
                    (sender, e) =>
                    {
                        this.StartActivity(typeof(BasicBookTaxiActivity));
                        this.Finish();
                    })
                    .SetNegativeButton("No", (sender, e) => this.Finish());

            AlertDialog alert = builder.Create();
            alert.Show();
        }
    }
}